<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                              |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\AuctionOrder;

use AuctionCore\System\AuctionException;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Other\Address;

/**
 * 回车类
 * @author Angl
 */
class CarReturn
{
    /**
     * 回车记录
     * @param $data
     * @return mixed
     */
    public static function create($data)
    {
        $data['create_time'] = date('Y-m-d H:i:s');
        $id = M('car_return_log')->add($data);
        return $id;
    }

    /**
     * 通过车辆id更新
     * @param $car_id
     * @param $data
     * @return
     */
    public function update_by_order_id($order_id,$data)
    {
        if($order_id<1 || empty($data)) return false;
        return M('car_return_log')->where('order_id='.$order_id)->save($data);
    }

    /**
     * 获取回车详细信息
     * @param $car_id
     * @return bool|mixed
     */
    public static function get_detail_by_order_id($order_id)
    {
        if(intval($order_id)<1) return false;
        return M('car_return_log')->where("order_id=".$order_id)->find();
    }

    /**
     * 获取列表
     * @param $page 当前页
     * @param $limit 每页限制
     * @param $dealer_id 车商ID
     * @return array
     */
    public function get_list($page, $limit, $dealer_id)
    {
        $page = intval($page) > 0 ? intval($page) : 1;
        $limit = intval($limit) > 0 ? intval($limit) : 10;
        $map = [];
        $map['crl.status'] = array('eq',0);//0待回车
        $map['c.car_source'] = array('eq',1);//暂时仅支持4S店的车
        $map['da.dealer_id'] = array('eq',$dealer_id);//暂时仅支持4S店的车

        $cols = "o.order_id,o.order_no,o.bidding_end_time,c.composite_state,c.accident_level,
            c.car_id,c.series_id,c.plate_prefix,c.plate_suffix,c.car_cc,c.turbo,c.gearbox,c.location_area,c.transfer_limit";
        $prefix = C("DB_PREFIX");

        $total = M('car_return_log')->alias("crl")
            ->join($prefix."order as o ON o.order_id=crl.order_id")
            ->join($prefix."cars as c ON c.car_id=o.car_id")
            ->join($prefix."car_owner as co ON co.owner_id=c.owner_id")
            ->join($prefix."dealer_auctioned as da ON da.order_id=o.order_id")
            ->where($map)->count();

        $list =M('car_return_log')->alias("crl")->field($cols)
            ->join($prefix."order as o ON o.order_id=crl.order_id")
            ->join($prefix."cars as c ON c.car_id=o.car_id")
            ->join($prefix."car_owner as co ON co.owner_id=c.owner_id")
            ->join($prefix."dealer_auctioned as da ON da.order_id=o.order_id")
            ->order("o.bidding_end_time desc,o.order_id desc")
            ->where($map)->page($page)->limit($limit)->select();
        //echo M()->getLastSql();
        if(!empty($list)){
            $cm = new CarModel();
            $addr = new Address();
            foreach ($list as $key => $val) {
                //车型
                $series = $cm->get_model_by_id($val['series_id'], 'series');
                $city_name = $addr->get_name_by_code($val['location_area']);
                $list[$key]['model'] = '['.$city_name.'] '.$series['name'] . ' ' . $val['car_cc'] . ($val['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $val['gearbox']);
            }
        }

        $data = ['total'=>$total,'list'=>$list];
        return $data;
    }

    /**
     * 获取车商待回车车辆总数
     * @param $dealer_id 车商ID
     * @return int
     */
    public function getCount($dealer_id){
        $prefix = C("DB_PREFIX");

        $map = array();
        $map['ada.dealer_id'] = array('eq', $dealer_id);
        $map['c.car_source'] = array('eq', 1);//暂时只取4S店的车2016.06.20

        $total = M('car_return_log')->alias("crl")
            ->join($prefix."order as o ON o.order_id=crl.order_id")
            ->join($prefix."cars as c ON c.car_id=o.car_id")
            ->join($prefix."car_owner as co ON co.owner_id=c.owner_id")
            ->join($prefix."dealer_auctioned as ada ON ada.order_id=o.order_id")
            ->where($map)->count();

       return $total;
    }
}

//end class
